﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using TiEntityObject.Transport;

namespace TI_EntityLib.TiEntityObjects.DayLog
{
   /// <summary>
   /// Полная информация о дневной активности
   /// </summary>
  
   public  class DayLog
    {
       private static List<DayLog> instances = null; 
       
       public static DayLog GetInstance(DateTime day)
        {
          ///ищем в списках
          foreach (DayLog i in instances )
          {
            if(i.Day.DayOfYear ==day.DayOfYear )
                return i;
         }
          //пытаемся прогрузиться
           DayLog dl = DayLog.Load(day);
           if(null==dl)
               dl=new DayLog(day);

            instances.Add (dl);
            return dl;
        }

       static DayLog()
       {
           instances = new List<DayLog>();
       }

       public DayLog(DateTime day) {
           this.Day = day;
           this.Transport = new DayLogTransport();
           }
    
       public DayLogTransport Transport { get; set; }

       /// <summary>
       /// Статистика посчитаная по каталогу
       /// </summary>
       public DayStatistic DayStatistic { get; set; }
    
       public DayLogDocumentDownload DocumentDownload { get; set; }
      
       public DateTime Day { get; set; }
       
       public long iDay { get { return Helper.Time.GetDateInMS(Day); } }

       public static DayLog Load(DateTime day)
       {
          var f = Helper.MongoTools.Instance.GetEntity<DayLog>("db.DayLog.findOne({iDay:"+Helper.Time.GetDateInMS(day).ToString() +" })");
          if (null == f)
              return null;

          return (DayLog)f; 
       }

       public void Save()
       {
           string script = "mergeDayLog(" + this.ToString() + ")";
           Helper.MongoTools.Instance.ExecuteScript(script);
       }

       public void UpdateTransportFile(TransportFile file) {
           string script = "updateTransportFile("+this.iDay.ToString ()+","  + file.ToString() + ")";
           Helper.MongoTools.Instance.ExecuteScript(script);

       }

       public override string ToString()
       {
           string s = Helper.Json<DayLog>.NewtonSerializeObject(this);
           return s;
       }
    }
 
   /// <summary>
   /// Информация о загрузке документов      
   /// </summary>
   public class DayLogDocumentDownload { } 
}
